angular.module("userapp")
.factory('loginService', function($http, $location, sessionService){
	return{
		submitLogin:function(user, $scope){

			$http({
				url: base_url + 'index.php/user/getLogin',
				method: "POST",
				data: $.param({user: $scope.user}),
				headers: {'Content-Type': 'application/x-www-form-urlencoded'}
			})
			.then(function(promise) {

				$scope.user = promise.data;
				var uid = promise.data;
				if(uid == 'Đăng nhập thành công'){
					$location.path('/profile');
				}
				else {
					$location.path('/home');
					console.log(uid);
				}
			});
		},
		submitLogout:function(){
			sessionService.destroy('uid');
			$location.path('/login');
		},
		islogged:function(){
			var $checkSessionServer = $http.post(base_url + 'index.php/user/check_session');
			return $checkSessionServer;
			/*
			if(sessionService.get('user')) return true;
			else return false;
			*/
		}
	}
});